import { createStore } from 'vuex'
import * as types from './action'
import * as api from '../api'
export default createStore({
  state: {
    planList: [],
  },
  getters: {
  },
  mutations: {
    [types.GET_PLAN_LIST] (state, payload) {
      state.planList = payload
    },
    [types.ADD_PLAN] (state, payload) {
      state.planList = [...state.planList, payload]
    },
    [types.DELETE_PLAN] (state, payload) {
      state.planList = state.planList.filter(item => item._id !== payload._id)
    },
  },
  actions: {
    // restful api 根据方法不同返回不同的资源
    async [types.GET_PLAN_LIST] ({ commit }, payload) {
      const result = await api.getPlanList(payload)
      commit(types.GET_PLAN_LIST, result)
    },
    async [types.ADD_PLAN] ({ commit }, payload) {
      const result = await api.addPlanList(payload)
      commit(types.ADD_PLAN, payload)
    },
    async [types.DELETE_PLAN] ({ commit }, payload) {
      const result = await api.deletePlanList(payload._id)
      commit(types.DELETE_PLAN, payload)
    },
  },
  modules: {
  }
})
